/*
 * Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
 */
package cn.player.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.ViewGroup;

import java.util.List;

public class TabFragmentPagerAdapter extends FragmentStatePagerAdapter {

    private List<Fragment> fragments;
    private List<String> items;

    public TabFragmentPagerAdapter(FragmentManager supportFragmentManager, List<Fragment> fragments, List<String> items) {
        super(supportFragmentManager);
        this.fragments = fragments;
        this.items = items;
    }

    public void setFragments(List<Fragment> fragments) {
        this.fragments = fragments;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if (items != null && items.size() > 0) {
            return items.get(position);
        }
        return null;
    }

    @Override
    public Fragment getItem(int position) {
        if (position >= fragments.size()) {
            return fragments.get(fragments.size() - 1);
        }
        return fragments.get(position);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        // TODO Auto-generated method stub
        super.setPrimaryItem(container, position, object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        // TODO Auto-generated method stub
        /*Fragment fragCache = (Fragment) super.instantiateItem(container, position);
        if (position < fragments.size()) {
            fragments.set(position, fragCache);
        }*/
        return super.instantiateItem(container,position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // TODO Auto-generated method stub
        super.destroyItem(container, position, object);
    }
}
